<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Column {

    private $width = "auto";
    private $background = "transparent";
    private $name = "";
    private $modules = array();
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function width($value = ""){
        if($value != "")
            $this->width = $value;
        return $this->width;
    }
    
    public function background($value = ""){
        if($value != "")
            $this->background = $value;
        return $this->background;
    }
    
    public function content(){
        foreach ($this->modules as $module){
            $module->show();
        }
    }
    
    public function addModule($module){
       $this->modules[$module->getName()] = $module;
    }
}

class Row {

    private $columns = array();
    private $name = "";
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function columns(){
        return $this->columns;
    }

    public function addColumn($name){
        $size = count($this->columns);
        $this->columns[count($this->columns)] = new Column($name);
        return $this->columns[$size];
    }
}

class Layout
{
	private $CI;
        private $rows = array();
        private $model = 'default';
	
/*
|===============================================================================
| Constructeur
|===============================================================================
*/
	
	public function __construct()
	{
		$this->CI =& get_instance();
	}
        
        public function rows(){
            return $this->rows;
        }
        
        public function addRow($name){
            $size = count($this->rows);
            $this->rows[$size] = new Row($name);
            return $this->rows[$size];
        }
}

/* End of file layout.php */
/* Location: ./application/libraries/layout.php */

class Header
{
    private $css_paths = array();
    
    public function __construct() {
        
    }
    
    public function addCSS($path){
        $css_paths[count($css_paths)] = $path;
    }
    
    public function addCSSModule($module){
        foreach ($module->getCSSFiles() as $value) :
            $css_paths[count($css_paths)] = MODULE_REPOSITORY . '/' . $module->getName() . '/' . $module->getCSSDirectory() . '/' . $value;
        endforeach;
    }
}

class Footer
{
    private $js_paths;
    
    public function addJS($path){
        
    }
}

class Template
{
	private $CI;
        private $var = array();
        private $theme = 'default';
/*
|===============================================================================
| Constructeur
|===============================================================================
*/
	
	public function __construct()
	{
		$this->CI =& get_instance();
                
                $this->var['header'] = '';
                $this->var['content'] = '';
                $this->var['footer'] = '';
                $this->var['layout'] = new Layout();

                //	Le titre est composé du nom de la méthode et du nom du contrôleur
                //	La fonction ucfirst permet d'ajouter une majuscule
                $this->var['title'] = ucfirst($this->CI->router->fetch_method()) . ' - ' . ucfirst($this->CI->router->fetch_class());

                //	Nous initialisons la variable $charset avec la même valeur que
                //	la clé de configuration initialisée dans le fichier config.php
                $this->var['charset'] = $this->CI->config->item('charset');
                $this->var['css'] = array();
                $this->var['js'] = array();
	}
        
        /*
|===============================================================================
| Méthodes pour modifier les variables envoyées au layout
|	. set_titre
|	. set_charset
|===============================================================================
*/
        public function set_title($title)
        {
                if(is_string($title) AND !empty($title))
                {
                        $this->var['title'] = $title;
                        return true;
                }
                return false;
        }

        public function set_charset($charset)
        {
                if(is_string($charset) AND !empty($charset))
                {
                        $this->var['charset'] = $charset;
                        return true;
                }
                return false;
        }
        
        /*
|===============================================================================
| Méthodes pour ajouter des feuilles de CSS et de JavaScript
|	. ajouter_css
|	. ajouter_js
|===============================================================================
*/
        public function ajouter_css($nom)
        {
                if(is_string($nom) AND !empty($nom) AND file_exists('./application/themes/' . $this->theme . '/css/' . $nom . '.css'))
                {
                        $this->var['css'][] = './application/themes/' . $this->theme . '/css/' . $nom . '.css';
                        return true;
                }
                return false;
        }

        public function ajouter_js($nom)
        {
                if(is_string($nom) AND !empty($nom) AND file_exists('./application/themes/' . $this->theme . '/javascript/' . $nom . '.js'))
                {
                        $this->var['js'][] = './application/themes/' . $this->theme . '/javascript/' . $nom . '.js';
                        return true;
                }
                return false;
        }
        
        public function set_theme($theme)
        {
                if(is_string($theme) AND !empty($theme) AND file_exists('/themes/' . $theme . '.php'))
                {
                        $this->theme = $theme;
                        return true;
                }
                return false;
        }
        
        public function getLayout(){
            return $this->var['layout'];
        }
	
/*
|===============================================================================
| Méthodes pour charger les vues
|	. view
|	. views
|===============================================================================
*/
	
        public function show()
	{
                $this->CI->load->view("../themes/". $this->theme."/header.php", $this->var);
		$this->CI->load->view("../themes/". $this->theme."/layout.php", $this->var);
                $this->CI->load->view("../themes/". $this->theme."/footer.php", $this->var);
	}
        
	public function view($name, $data = array())
	{
		$this->var['content'] .= $this->CI->load->view($name, $data, true);
                $this->CI->load->view("../themes/". $this->theme."/header.php", $this->var);
		$this->CI->load->view("../themes/". $this->theme."/layout.php", $this->var);
                $this->CI->load->view("../themes/". $this->theme."/footer.php", $this->var);
	}
	
	public function views($name, $data = array())
	{
		$this->var['content'] .= $this->CI->load->view($name, $data, true);
		return $this;
	}
}

/* End of file layout.php */
/* Location: ./application/libraries/layout.php */
